package com.year_23.may;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: jsh
 * @create: 2023/5/23 22:11
 * @Description:
 * @FileName: Day23
 * @History:
 * @自定义内容：
 */
public class Day23 {

    public int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) {
        int n = values.length;
        Integer[] sort = new Integer[n];
        for (int i = 0; i < n; i++) {
            sort[i] = i;
        }
        Arrays.stream(sort)
                .sorted( (a, b) -> values[b] - values[a])
                .collect(Collectors.toList()).toArray(sort);
        int res = 0, num = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n && num < numWanted; i++) {
            if (map.getOrDefault(labels[sort[i]], 0) < useLimit) {
                map.put(labels[sort[i]], map.getOrDefault(labels[sort[i]], 0) + 1);
                res += values[sort[i]];
                num ++;
            }
        }
        return res;
    }
}
